package com.kyzh.core.pager.weal.community

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.viewModels
import com.blankj.utilcode.util.KeyboardUtils
import com.gushenge.core.beans.CommunityListBean
import com.jeremyliao.liveeventbus.LiveEventBus
import com.kingja.loadsir.core.LoadSir
import com.kyzh.core.R
import com.kyzh.core.databinding.ActCommonitylistBinding
import com.gushenge.core.base.fragment.BaseFragment
import com.kyzh.core.pager.weal.community.adapter.CommunityListAdapter
import com.kyzh.core.pager.weal.community.viewmodel.CommunityViewModel
import com.kyzh.core.utils.setVisibility
import com.kyzh.core.utils.showError
import org.jetbrains.anko.support.v4.onRefresh

class DialogCommunityListFragment : BaseFragment() {
    companion object{
        fun newInstance(type:Int): DialogCommunityListFragment{
            val fragment = DialogCommunityListFragment()
            fragment.arguments =  Bundle().apply {
                putInt("type",type)
            }
            return fragment
        }
    }
    private val adapter = CommunityListAdapter()
    private lateinit var db:ActCommonitylistBinding
    private val vm by viewModels<CommunityViewModel>()
    private var mp  = 1
    private var type  = 1
    private var keyword = ""
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        db = ActCommonitylistBinding.inflate(layoutInflater)
        loadSir = LoadSir.getDefault().register(db.rev) {
            reloadClick()
        }
        type = arguments?.getInt("type")!!+1
        db.lifecycleOwner = this
        return db.root
    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        db.rev.adapter = adapter
        getData()
        db.searchClose.setVisibility(false)
        db.swipe.onRefresh {
            mp = 1
            getData()
        }
        adapter.loadMoreModule.setOnLoadMoreListener {
            mp ++
            vm.getData(mp,type,keyword){
                db.swipe.isRefreshing = false
                adapter.addData(this.data)
                adapter.loadMoreModule.loadMoreComplete()
                if(mp>=maxP){
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
            }
        }
        db.edittext.addTextChangedListener {
            keyword = db. edittext.text.toString().trim()
        }
        db.edittext.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                KeyboardUtils.hideSoftInput(db.edittext)
                val words =db. edittext.text.toString().trim()
                mp = 1
                if (words.isNotEmpty()) {
                    vm.getData( mp,type,words){
                        db.swipe.isRefreshing = false
                        adapter.setNewInstance(this.data)
                    }
                }else{
                    vm.getData( mp,type){
                        adapter.setNewInstance(this.data)
                    }
                }
            }
            true
        }
        adapter.setOnItemClickListener{a,d,c->
            LiveEventBus.get<Int?>("communitylist").post(1)

            val data = a.data[c] as CommunityListBean.Data
//            val pair = Pair<View, String>(d.findViewById(R.id.icon), "icon")
//            val pair1 = Pair<View, String>(d.findViewById(R.id.tvName), "tvName")
//            val pair2= Pair<View, String>(d.findViewById(R.id.ivGuanzhu), "ivGuanzhu")
            CommunityActivity.start(requireActivity(),data.id )
        }
        adapter.setOnItemChildClickListener{a,b,c->
            when(b.id){
                R.id.ivGuanzhu ->{
                    val data = a.data[c] as CommunityListBean.Data
                    vm.guanzhu(data.id) {
                        data.isGuanzhu = !data.isGuanzhu
                        adapter.notifyItemChanged(c)
                    }
                }
            }
        }
        db.tvSearch.setOnClickListener {
            KeyboardUtils.hideSoftInput(db.edittext)
            getData()
        }
        adapter.setEmptyView(R.layout.empty)
    }
    private fun reloadClick() {
        getData()
    }
    private fun getData(){
        vm.getData(mp,type,keyword){
            db.swipe.isRefreshing = false
            if(this.code == 1){
                loadSir.showSuccess()
                adapter.setNewInstance(this.data)
                if(mp>=maxP){
                    adapter.loadMoreModule.loadMoreEnd(true)
                }
            }else{
                loadSir.showError("请重试")
            }
        }
    }
}